/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Rules;

import org.htmlparser.Tag;
import org.htmlparser.Node;
import org.htmlparser.tags.*;
import org.htmlparser.util.*;
import Database.*;

/**
 *
 * @author Sinem
 */
public class TagRule extends BaseRule{
   private ResultTableItem result;
   public TagRule(Tag tag)
   {
        super();
        m_Tag = tag;
        result = new ResultTableItem();
   }

   @Override
   public void check()
   {      

       result.sWebPage = this.sWebPage;
       result.nLineNumber = this.getLineNumber();
       //now check the rule and write the result to db
       if(m_Tag.getClass().getName().compareTo("org.htmlparser.tags.HeadTag") == 0)
       {
          this.check((HeadTag) m_Tag);
       }
       else if(m_Tag.getClass().getName().compareTo("org.htmlparser.tags.ImageTag") == 0)
       {
          this.check((ImageTag) m_Tag);
       }
       else if(m_Tag.getClass().getName().compareTo("org.htmlparser.tags.TableTag") == 0)
       {
          this.check((TableTag) m_Tag);
       }
       else if(m_Tag.getClass().getName().compareTo("org.htmlparser.tags.InputTag") == 0)
       {
          this.check((InputTag) m_Tag);
       }
       else if(m_Tag.getClass().getName().compareTo("org.htmlparser.tags.FrameTag") == 0)
       {
          this.check((FrameTag) m_Tag);
       }
       else if(m_Tag.getClass().getName().compareTo("org.htmlparser.tags.LinkTag") == 0)
       {
          this.check((LinkTag) m_Tag);
       }
   }
   
   private void check(HeadTag headTag)
   {
        Node node;
        SimpleNodeIterator iterator = headTag.elements();
        boolean bTitleTagFound = false;
        boolean bMetaTagFound = false;
        String attribute;
        int flags = 0;
        while(iterator.hasMoreNodes())
        {
          node = iterator.nextNode();
          // Rule 1-->Each head tag should include title tag
          if (node.getClass().getName().compareTo("org.htmlparser.tags.TitleTag")== 0)
          {
              bTitleTagFound = true;
              TitleTag tag = (TitleTag) node;
              String titleStr = tag.getStringText();
              result.sRuleId = "Tag02";
              if (titleStr.length() > 60){                 
                  result.bRuleViolated = true;
              }
              else{
                  result.bRuleViolated = false;
              }
              Manager.getInstance().storeToDB(result);
          }
          //Rule 2--> Meta tags with name attribute(description, keywords,author) should exist
          if (node.getClass().getName().compareTo("org.htmlparser.tags.MetaTag")== 0)
          {
             bMetaTagFound = true;//TODO: bunu ana sayfa i?in ?zelle?tirmemiz gerekecek
             attribute = ((MetaTag)node).getAttribute("name");
             if (attribute != null)
             {
                if (attribute.compareToIgnoreCase("description") == 0)
                  flags |= 1;
                if (attribute.compareToIgnoreCase("keywords") == 0)
                  flags |= 2;
                if (attribute.compareToIgnoreCase("author") == 0)
                  flags |= 4;
             }
          }
        }
        if (bTitleTagFound == false)
        {           
            result.bRuleViolated = true;
            result.sRuleId = "Tag01";
            Manager.getInstance().storeToDB(result);
        }else
        {
            result.sRuleId = "Tag01";
            result.bRuleViolated = false;
            Manager.getInstance().storeToDB(result);
        }
        
        if (bMetaTagFound == false)
        {
            result.bRuleViolated = true;
            result.sRuleId = "Tag03";
            Manager.getInstance().storeToDB(result);
        }
        else
        {
            result.bRuleViolated = false;
            result.sRuleId = "Tag03";
            Manager.getInstance().storeToDB(result);
        }

        if (flags != 8)
        {           
            result.bRuleViolated = true;
            result.sRuleId = "Tag04";
            Manager.getInstance().storeToDB(result);
        }else
        {
            result.bRuleViolated = false;
            result.sRuleId = "Tag04";
            Manager.getInstance().storeToDB(result);
        }
    }
   
   private void check(ImageTag imgTag)
    {
        String altAttribute;

        altAttribute = imgTag.getAttribute("alt");
        // validator E800
        if(altAttribute == null)
        {           
           result.bRuleViolated = true;
           result.sRuleId = "Tag05";
           Manager.getInstance().storeToDB(result);
        }
        else        
        {
            result.bRuleViolated = false;
            result.sRuleId = "Tag05";
            Manager.getInstance().storeToDB(result);

            ///////////////////////////////////////////
            result.sRuleId = "Tag06";
            if (altAttribute.length() > 100)
               result.bRuleViolated = true;
            else
               result.bRuleViolated = false;

            Manager.getInstance().storeToDB(result);
            
            ///////////////////////////////////////////
            result.sRuleId = "Tag07";
            if (altAttribute.endsWith(". ") == false)
              result.bRuleViolated = true;             
            else
               result.bRuleViolated = false;

            Manager.getInstance().storeToDB(result);
            
        }
    }
    private void check(TableTag tableTag)
    {
        String attribute;

        attribute = tableTag.getAttribute("style");
        result.sRuleId = "Tag08";
        if((attribute != null) && (attribute.contains("background-image:") == true))
          result.bRuleViolated = true;
        else        
            result.bRuleViolated = false;        
        
        Manager.getInstance().storeToDB(result);

        //////////////////////////////////////////////
        String tableTagContent;
        tableTagContent = tableTag.getChildrenHTML();
        result.sRuleId = "Tag10";//nested table

        if (tableTagContent.contains("<table") == true)           
            result.bRuleViolated = true;            
        else        
            result.bRuleViolated = false;           

        Manager.getInstance().storeToDB(result);

        //////////////////////////////////////////////
        // validator E809
        result.sRuleId = "Tag11";
        if (tableTagContent.contains("<th") == true)                   
            result.bRuleViolated = true;
        else        
            result.bRuleViolated = false;        

        Manager.getInstance().storeToDB(result);


        //////////////////////////////////////////////
        result.sRuleId = "Tag12";//table should have end tag
        if (tableTag.getEndTag() == null)
            result.bRuleViolated = true;            
        else        
            result.bRuleViolated = false;       

        Manager.getInstance().storeToDB(result);

        //////////////////////////////////////////////
        
        attribute = tableTag.getAttribute("cols");
        result.sRuleId = "Tag13";
        if (attribute == null)
        {  result.bRuleViolated = true;  }
        else
        { result.bRuleViolated = false; }
        Manager.getInstance().storeToDB(result);
    }

    private void check(InputTag inputTag)
    {
        String typeAttribute;

        typeAttribute = inputTag.getAttribute("type");

        result.sRuleId = "Tag09";
        
        if((typeAttribute != null) && (typeAttribute.compareToIgnoreCase("radio") == 0))
          result.bRuleViolated = true;                        
        else
           result.bRuleViolated = false;
        
        Manager.getInstance().storeToDB(result);
    }

    private void check(FrameTag frameTag)
    {
        String titleAttribute;

        result.sRuleId = "Tag14";

        titleAttribute = frameTag.getAttribute("title");

        if(titleAttribute == null)        
            result.bRuleViolated = true;                       
        else
            result.bRuleViolated = false;

        Manager.getInstance().storeToDB(result);
    }

    private void check(LinkTag linkTag)
    {
        String linkTagContent;

        linkTagContent = linkTag.toString();

        result.sRuleId = "Tag15";

        if(linkTagContent.contains("<img") == true)        
            result.bRuleViolated = true;                        
        else 
            result.bRuleViolated = false;

        Manager.getInstance().storeToDB(result);
    }
}
